# -*- encoding:utf-8 -*-
# __author__ = 'chenyongbing'
# __poject__ = 'ops_platform'
# __datetime__ = '2022/8/3'
# __name__ = 'signals.py'
from django.db.models.signals import post_save
from django.dispatch import receiver
from .models import WorkOrder
from apps.cicd.handles.handles import workorder_deployment_exists, workorder_yaml_format
import logging
logger = logging.getLogger(__name__)
# Create your code here.




@receiver(post_save, sender=WorkOrder)
def init_workorder_yaml(sender, instance, created, *args, **kwargs):
    '''
    工单初始化yaml
    1. 判断工单类型
    2. 判断是否存在deployment
    3. 生成yaml
    :param sender:
    :param instance:
    :param created:
    :param args:
    :param kwargs:
    :return:
    '''
    if created and instance.order_type == 'update' and instance.nodeploy == 0:
        if workorder_deployment_exists(instance) != True:
            logging.info("{}-deployment not exists".format(instance.service.code))
            workorder_yaml_format(instance)






